package com.wisezone.frame.web;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.OnlineUserMgr;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.web.BaseController;
import com.wisezone.frame.service.FrLoginService;
import com.wisezone.permission.bean.entity.WzUserEntity;
import com.wisezone.permission.service.WzUserService;

@SuppressWarnings("unchecked")
public class FrLoginController extends BaseController {

	private WzUserService wzUserService;
	
	private FrLoginService frLoginService;
	
	/**
	 * 进入登录
	 * @return
	 * @throws WzWebException
	 */
	public String enterLogin() throws WzWebException{
		return "login";
	}
	
	/**
	 * 登录
	 * @return
	 * @throws WzWebException
	 */
	public String login() throws WzWebException{
		
		String loginName = request.getParameter("loginName");
		String password = request.getParameter("password");
		if(StringUtils.isEmpty(loginName)){
			this.addErrMsg("用户名不能为空");
			return "login";
		}
		if(StringUtils.isEmpty(password)){
			this.addErrMsg("密码不能为空");
			return "login";
		}
		try {
			
			WzUserEntity entity = this.wzUserService.login(loginName, password);
	        if(entity == null){
	        	this.addErrMsg("用户名或密码错误");
	        	return "login";
	        }
	        
	        //用户存储处理
	        frLoginService.userInject(session.getId(),entity);
	        
	        return "frame/index";
		} catch (Exception e) {
			this.addErrMsg("登陆失败");
			throw new WzWebException(e);
		}
	}
	
	/**
	 * 注销
	 * @return
	 * @throws WzWebException
	 */
	public String loginout() throws WzWebException{
		
		if(session != null){
			this.frLoginService.logout(session);
		}
		return "login";
	}
	
	

	public void setWzUserService(WzUserService wzUserService) {
		this.wzUserService = wzUserService;
	}

	public void setFrLoginService(FrLoginService frLoginService) {
		this.frLoginService = frLoginService;
	} 
	
	
}
